package net.pierrerenaudin.blobbyvolley.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.canvas.dom.client.FillStrokeStyle;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.Image;


public class RenderManager {

	private final static int width = 800;
	private final static int height = 600;

	private Canvas canvas;
	private Context2d context;

	private List<Image> blobby1Img;
	private List<Image> blobby2Img;
	private List<Image> fontImg;

	private Image backgroundImg;
	private Image ballImg;
	private Image pokalImg;
	public  Image titleImg;

	private GameController controller;

	public RenderManager(GameController controller){
		this.controller = controller;

		canvas = Canvas.createIfSupported();
		canvas.setCoordinateSpaceHeight(height);
		canvas.setCoordinateSpaceWidth(width);
		canvas.getElement().getStyle().setWidth(width, Unit.PX);
		canvas.getElement().getStyle().setHeight(height, Unit.PX);
		context = canvas.getContext2d();

		// Load blobby image
		blobby1Img = new ArrayList<Image>();
		blobby2Img = new ArrayList<Image>();
		for(int i = 0; i < 5; i++)
		{
			blobby1Img.add(i, new Image("data/img/" + i + "b.png"));
			blobby2Img.add(i, new Image("data/img/" + i + "r.png"));
		}

		// Load font images
		fontImg = new ArrayList<Image>();
		for(int i = 0; i < 11; i++)
		{
			fontImg.add(i, new Image("data/img/f" + i + ".png"));
		}

		// Load images
		backgroundImg = new Image("data/img/strand1.png");
		backgroundImg.addLoadHandler(new LoadHandler() {
			@Override
			public void onLoad(LoadEvent event) {
				GWT.log("gwt-debug : Background image loaded");
			}
		});
		pokalImg = new Image("data/img/pokal.png");
		ballImg = new Image("data/img/ball01.png");
		titleImg = new Image("data/img/titel.png");
	}

	public Image getTitleImg() {
		return titleImg;
	}

	public List<Image> getFontImg() {
		return fontImg;
	}

	public Image getPokalImg() {
		return pokalImg;
	}

	public void drawBackgroundMenu(){
		context.setFillStyle("rgb(0, 0, 0)");
		context.fillRect(0, 0, 800, 600);
		context.setGlobalAlpha(0.2);
		context.drawImage(asImageElement(backgroundImg), 0, 0);
		context.setGlobalAlpha(1);
		context.drawImage(asImageElement(titleImg), 20, 80); 
	}

	public void drawBackgroundWin(Player player){
		context.setFillStyle("rgb(0, 0, 0)");
		context.fillRect(0, 0, 800, 600);
		context.setGlobalAlpha(0.2);
		context.drawImage(asImageElement(backgroundImg), 0, 0);
		context.setGlobalAlpha(1);
		context.drawImage(asImageElement(pokalImg), 20, 80); 
		if(player.side == Player.LEFT_PLAYER){
			context.drawImage(asImageElement(blobby1Img.get(0)), 300, 150); 
		}
		else{
			context.drawImage(asImageElement(blobby2Img.get(0)), 300, 150); 
		}
	}

	public void drawGame(){
		Player leftPlayer = controller.getLeftPlayer();
		Player rightPlayer = controller.getRightPlayer();
		Ball ball = controller.getBall();

		context.drawImage(asImageElement(backgroundImg), 0, 0);
		context.fillRect(ball.position.x - 2, 5, 5, 5);
		context.drawImage(asImageElement(ballImg), ball.position.x - Ball.BALL_RADIUS, ball.position.y - Ball.BALL_RADIUS);
		context.drawImage(
				asImageElement(blobby1Img.get((int) Math.round(leftPlayer.state))),
				leftPlayer.position.x - Player.width / 2,
				leftPlayer.position.y -  Player.height / 2);

		context.drawImage(
				asImageElement(blobby2Img.get((int) Math.round(rightPlayer.state))),
				rightPlayer.position.x - Player.width / 2,
				rightPlayer.position.y -  Player.height / 2);

		context.setFillStyle("rgb(255, 255, 255)");

		if(rightPlayer.score > 9) {
			context.drawImage(asImageElement(fontImg.get(1)), 724, 20);
			context.drawImage(asImageElement(fontImg.get(rightPlayer.score - 10)), 748, 20);
		} else {
			context.drawImage(asImageElement(fontImg.get(0)), 724, 20);
			context.drawImage(asImageElement(fontImg.get(rightPlayer.score)), 748, 20);
		}

		if(leftPlayer.score > 9)
		{
			context.drawImage(asImageElement(fontImg.get(1)), 44, 20);
			context.drawImage(asImageElement(fontImg.get(leftPlayer.score - 10)), 68, 20);
		} else {
			context.drawImage(asImageElement(fontImg.get(0)), 44, 20);
			context.drawImage(asImageElement(fontImg.get(leftPlayer.score)), 68, 20);
		}


		if(rightPlayer.isServing())
			context.drawImage(asImageElement(fontImg.get(10)), 700, 20);
		else if(leftPlayer.isServing())
			context.drawImage(asImageElement(fontImg.get(10)), 20, 20);
	}

	private ImageElement asImageElement(Image img){
		return (ImageElement) img.getElement().cast();
	}

	public Canvas getCanvas(){
		return canvas;
	}

}
